<template>
  <el-dialog
    :title="!dataForm.orgid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="组织名称" prop="orgname">
      <el-input v-model="dataForm.orgname" placeholder="组织名称"></el-input>
    </el-form-item>
    <el-form-item label="组织类型" prop="orgtype">
      <el-input v-model="dataForm.orgtype" placeholder="组织类型"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="orgaddress">
      <el-input v-model="dataForm.orgaddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="orgboss">
      <el-input v-model="dataForm.orgboss" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="orgemail">
      <el-input v-model="dataForm.orgemail" placeholder="邮箱"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orgid: 0,
          orgname: '',
          orgtype: '',
          orgaddress: '',
          orgboss: '',
          orgemail: ''
        },
        dataRule: {
          orgname: [
            { required: true, message: '组织名称不能为空', trigger: 'blur' }
          ],
          orgtype: [
            { required: true, message: '组织类型不能为空', trigger: 'blur' }
          ],
          orgaddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          orgboss: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          orgemail: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orgid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orgid) {
            this.$http({
              url: this.$http.adornUrl(`/orginfo/orginfo/info/${this.dataForm.orgid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orgname = data.orginfo.orgname
                this.dataForm.orgtype = data.orginfo.orgtype
                this.dataForm.orgaddress = data.orginfo.orgaddress
                this.dataForm.orgboss = data.orginfo.orgboss
                this.dataForm.orgemail = data.orginfo.orgemail
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/orginfo/orginfo/${!this.dataForm.orgid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orgid': this.dataForm.orgid || undefined,
                'orgname': this.dataForm.orgname,
                'orgtype': this.dataForm.orgtype,
                'orgaddress': this.dataForm.orgaddress,
                'orgboss': this.dataForm.orgboss,
                'orgemail': this.dataForm.orgemail
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
